package com.tom.architect.party3.day31;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.tom.architect.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity31 extends AppCompatActivity {
    private static final String TAG = "MainActivity31";
    private ImageView action_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main31);
        action_image = findViewById(R.id.action_image);

        Log.e(TAG, "onCreate: "+ Thread.currentThread().getName());
        doJust();
    }

    // 访问网络图片。
    private void doJust() {
        String urlStr = "http://pic-bucket.ws.126.net/photo/0003/2024-01-12/IO8BIBJV00AJ0003NOS.jpg";

        Observable.just(urlStr) // ObservableJust对象。
                .map(new Function<String, Bitmap>() { // ObservableMap： 他的source是上面的ObservableJust对象。
                    @Override
                    public Bitmap apply(String urlStr) throws Exception {
                        Log.e(TAG, "apply1: "+ Thread.currentThread().getName());

                        // 第五步：下载图片
                        URL url = new URL(urlStr);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        InputStream inputStream = urlConnection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        return bitmap;
                    }
                })
                .map(new Function<Bitmap, Bitmap>() { // ObservableMap对象包裹。
                    @Override
                    public Bitmap apply(Bitmap bitmap) throws Exception {
                        // 2.加一个水印(中间可以添加图片缓存...)
                        bitmap = createWatermark(bitmap, "2230 RxJava2.0");
                        Log.e(TAG, "apply2: "+ Thread.currentThread().getName());
                        return bitmap;
                    }
                })
                .map(new Function<Bitmap, Bitmap>() {
                    @Override
                    public Bitmap apply(Bitmap bitmap) throws Exception {
                        Log.e(TAG, "apply3: "+ Thread.currentThread().getName());
                        return bitmap;
                    }
                })
                .subscribeOn(Schedulers.io()) // 代表之前的所有方法，都在 子线程执行。可以使用别的策略。
                .observerOn(Schedulers.mainThread()) // 之后的在主线程中执行。

                .subscribe(new Consumer<Bitmap>() { // ObservableMap
                    @Override
                    public void onNext(Bitmap bitmap) throws Exception {
                        // 第七步：显示图片。
                        Log.e(TAG, "onNext: "+ Thread.currentThread().getName());
                        action_image.setImageBitmap(bitmap);
                    }
                });
    }


    private Bitmap createWatermark(Bitmap bitmap, String mark) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint p = new Paint();
        // 水印颜色
        p.setColor(Color.parseColor("#C5FF0000"));
        // 水印字体大小
        p.setTextSize(150);
        //抗锯齿
        p.setAntiAlias(true);
        //绘制图像
        canvas.drawBitmap(bitmap, 0, 0, p);
        //绘制文字
        canvas.drawText(mark, 0, h / 2, p);
        canvas.save();
        canvas.restore();
        return bmp;
    }

}